package com.ucenter.server.user.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.system.comm.model.Page;
import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameTimeUtil;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;
import com.ucenter.server.user.dao.UserLogDao;
import com.ucenter.server.user.enums.UserLogType;
import com.ucenter.server.user.pojo.UserLog;
import com.ucenter.server.user.service.UserLogService;

/**
 * user_log的Service
 * @author autoCode
 * @date 2018-02-06 15:18:19
 * @version V1.0.0
 */
@Component
public class UserLogServiceImpl implements UserLogService {

	@Autowired
	private UserLogDao userLogDao;
	
	public void save(UserLog userLog) {
		userLog.setUlId(FrameNoUtil.uuid());
		userLogDao.save(userLog);
	}

	@Override
	public UserLog get(String ulId) {
		return userLogDao.get(ulId);
	}

	@Override
	public ResponseFrame pageQuery(UserLog userLog) {
		userLog.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = userLogDao.findUserLogCount(userLog);
		List<UserLog> data = null;
		if(total > 0) {
			data = userLogDao.findUserLog(userLog);
		}
		Page<UserLog> page = new Page<UserLog>(userLog.getPage(), userLog.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(String ulId) {
		ResponseFrame frame = new ResponseFrame();
		userLogDao.delete(ulId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public ResponseFrame saveLogin(String userId, String ip) {
		ResponseFrame frame = new ResponseFrame();
		String remark = FrameTimeUtil.getStrTime() + "登录成功，操作IP为" + ip;
		UserLog userLog = new UserLog(UserLogType.LOGIN, userId, remark);
		save(userLog);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public ResponseFrame saveLogout(String userId, String ip) {
		ResponseFrame frame = new ResponseFrame();
		String remark = FrameTimeUtil.getStrTime() + "退出成功，操作IP为" + ip;
		UserLog userLog = new UserLog(UserLogType.LOGOUT, userId, remark);
		save(userLog);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public List<UserLog> findByUidTypeNum(String userId, Integer type, Integer num) {
		return userLogDao.findByUidTypeNum(userId, type, num);
	}
}